home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Selection / Multimedia Selection Volume One - CD-ROM / MULTIMEDIA SELECTION____________.ISO / programz / tsrlib / saver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-30  |  4.0 KB  |  210 lines

  1. /*        Screen Saver Clock.
  2.         -------------------
  3.  
  4.     Roger Dalton of R&D Associates.
  5.  
  6. */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #include "tsrlib.h"
  13.  
  14.  
  15. /* Table must exist! */
  16.  
  17. HOTKEY hotkey_table[] = {
  18.     LAST_KEY                    /* no hotkeys needed */
  19. };
  20.  
  21.  
  22. /* Signature must exist! */
  23.  
  24. char signature[] = "SCREEN SAVER";
  25.  
  26.  
  27. /* Structure to hold DOS time. */
  28.  
  29. typedef struct {
  30.     int hour;
  31.     int min;
  32.     int sec;
  33.     int hsec;
  34. } TIMES;
  35.  
  36.  
  37. /* Specify only what you need. If you won't need a heap, specify a
  38. small value like this as 0 means 'use all possible'. NOTE: Turbo C2.0
  39. user can't specify less than about 512 anyway! */
  40.  
  41. unsigned int _heaplen = 512;
  42. unsigned int _stklen = 256;
  43.  
  44.  
  45. volatile int screen_state = 1;    /* screen is visible */
  46. unsigned int far *screen;        /* pointer to video screen */
  47. int screen_timer = 0;            /* delay timer 1/2 seconds */
  48.  
  49. char screen_save[8000];            /* screen save buffer */
  50.  
  51.  
  52. /* PROTOTYPES */
  53.  
  54. void get_time(TIMES *timerp);
  55. int get_mode(void);
  56. int my_key_handler(int scancode, int shiftmask);
  57.  
  58.  
  59.  
  60. /* The main program. The only possibilites are:
  61.  
  62.     saver interval
  63. or
  64.     saver interval foreground
  65. or
  66.     saver interval foreground background
  67. */
  68.  
  69. main(argc, argv)
  70. int argc;
  71. char **argv;
  72. {
  73.     TIMES tim;                        /* time structure */
  74.     int mode;                        /* video mode */
  75.     unsigned int chr;                /* buffer for characters */
  76.     unsigned int attr = 0x7000;        /* screen sttribute */
  77.     unsigned int far *scp;            /* temp pointer to video screen */
  78.     int i;                            /* loop counter */
  79.     int flip = 0;                    /* colon ticker */
  80.     char tmp[32];                    /* time string buffer */
  81.     static char tick[] = ": ";
  82.     int old_sec = 0;
  83.     int screen_limit;                /* delay before saving */
  84.  
  85.     if (resident_tsr()) {
  86.         printf("%s is already resident.\n", signature);
  87.         exit(1);
  88.     }
  89.     printf("%s is loaded.\n", signature);
  90.  
  91.     keypressed = my_key_handler;
  92.  
  93.     if (argc > 1) {
  94.         screen_limit = atoi(argv[1]) << 1;
  95.     } else {
  96.         screen_limit = 60 * 2;
  97.     }
  98.  
  99.     if (argc > 2) {
  100.         attr &= 0xf000;
  101.         attr |= atoi(argv[2]) << 8;
  102.     }
  103.  
  104.     if (argc > 3) {
  105.         attr &= 0x0f00;
  106.         attr |= atoi(argv[3]) << 12;
  107.     }
  108.  
  109.     screen = (unsigned int far *)((get_mode() == 7)? 0xb0000000L: 0xb8000000L);
  110.  
  111.     /* Past here CAN'T use printf etc. */
  112.  
  113.     go_tsr(SLEEP, 1);
  114.  
  115.     while (1) {
  116.         mode = get_mode();
  117.         if (mode == 7 || mode == 3 || mode == 2) {
  118.  
  119.             if (screen_state) {
  120.  
  121.                 if (screen_timer++ > screen_limit) {
  122.                     movedata(FP_SEG(screen), 0, _DS, (unsigned)screen_save, 8000);
  123.                     for (i = 8000, scp = screen; i-- > 0;) {
  124.                         *scp++ = 0x0720;
  125.                     }
  126.                     screen_state = 0;
  127.                     goto sleep;
  128.                 }
  129.  
  130.                 if (mode == 7) {
  131.                     attr = 0x7000;
  132.                 }
  133.  
  134.                 get_time(&tim);
  135.                 if (tim.sec != old_sec) {
  136.                     flip ^= 1;                /* flip the colon */
  137.                 }
  138.                 old_sec = tim.sec;
  139.                 sprintf(tmp, " %02d%c%02d%c%02d ", tim.hour, tick[flip],
  140.                         tim.min, tick[!flip], tim.sec);
  141.  
  142.                 for (i = 0; (chr = tmp[i]); i++) {
  143.                     screen[35 + i] = chr | attr;
  144.                 }
  145.             }
  146.         }
  147. sleep:
  148.         suspend_tsr(SLEEP, 9);
  149.     }
  150. }
  151.  
  152.  
  153.  
  154.  
  155. /* Function to read DOS date & time, and build the structure. */
  156.  
  157. void get_time(TIMES *timerp)
  158. {
  159.     union REGS regs;
  160.  
  161.     regs.h.ah = 0x2c;
  162.     int86(0x21, ®s, ®s);
  163.     timerp->hour = regs.h.ch;
  164.     timerp->min = regs.h.cl;
  165.     timerp->sec = regs.h.dh;
  166.     timerp->hsec = regs.h.dl;
  167. }
  168.  
  169.  
  170.  
  171.  
  172. /* Return the current display mode. */
  173.  
  174. int get_mode(void)
  175. {
  176.     _AH = 0x0f;
  177.     geninterrupt(0x10);
  178.     return _AL;
  179. }
  180.  
  181.  
  182.  
  183.  
  184. /* Hendler for keypresses. Eats the key which lights up the screen. */
  185.  
  186. int my_key_handler(int scancode, int shiftmask)
  187. {
  188.     screen_timer = 0;                        /* clear on every key */
  189.     if (screen_state) {
  190.         return 0;
  191.     } else {
  192.         screen_state = 1;
  193.         movedata(_DS, (unsigned)screen_save, FP_SEG(screen), 0, 8000);
  194.         _TS_tic_count = _TS_bg_limit + 1;    /* instant timeout! */
  195.  
  196.         /* don't eat non-typing keys */
  197.  
  198.         switch (scancode) {
  199.  
  200.         case 29:                /* caps lock */
  201.         case 42:                /* left shift */
  202.         case 56:                /* ctrl */
  203.         case 54:                /* right shift */
  204.         case 58:                /* alt */
  205.             return 0;
  206.         }
  207.         return EAT_KEY;
  208.     }
  209. }
  210.